package apy.codecase.business.view.fragment;

import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import apy.codecase.R;
import apy.codecase.business.presenter.PswImpl;
import apy.codecase.business.view.activity.AddPswActivity;
import apy.codecase.listener.OnLoadDataListener;
import apy.codecase.model.PswDaoModel;
import apy.codecase.rv.BaseListFragment;
import apy.codecase.rv.BaseViewHolder;
import apy.codecase.rv.ILayoutManager;
import apy.codecase.rv.MyStaggeredGridLayoutManager;
import butterknife.BindView;

import static apy.codecase.rv.PullRecycler.ACTION_PULL_TO_REFRESH;

/**
 * Created by Administrator on 2017/10/25.
 * 密码箱
 */

public class PswFragment extends BaseListFragment<List<PswDaoModel>, PswDaoModel>  implements OnLoadDataListener {

    @BindView(R.id.psw_fab)
    FloatingActionButton pswFab;

    public static final int REQUEST_ADD_PSW = 100;
    public static final String REQUEST_ADD_PSW_TYPE = "model";
    private List<PswDaoModel> mList = new ArrayList<>();

    @Override
    protected BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        return new PswViewHolder(View.inflate(parent.getContext(), R.layout.item_psw_fragment, null));
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_psw;
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new PswImpl(getActivity(), this);
        recycler.setRefreshing();
        recycler.enableLoadMore(false);
        pswFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddPswActivity.class);
                startActivityForResult(intent,REQUEST_ADD_PSW);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null && requestCode == REQUEST_ADD_PSW){
            PswDaoModel model= (PswDaoModel) data.getSerializableExtra(REQUEST_ADD_PSW_TYPE);
            mList.clear();
            mList.add(model);
            mDataList.addAll(mList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onRefresh(int action) {
        if(action == ACTION_PULL_TO_REFRESH){
            presenter.loadData();
        }
    }

    @Override
    protected ILayoutManager getLayoutManager() {
        return new MyStaggeredGridLayoutManager(3, GridLayoutManager.VERTICAL);
    }

    @Override
    public void afterLoadData(List<PswDaoModel> t) {
        super.afterLoadData(t);
        mDataList.clear();
        mDataList.addAll(t);
        adapter.notifyDataSetChanged();
        recycler.onRefreshCompleted();
    }

    class PswViewHolder extends BaseViewHolder {
        @BindView(R.id.psw_title)
        TextView pswTitle;
        @BindView(R.id.psw_websit)
        TextView pswWebsit;
        @BindView(R.id.psw_account)
        TextView pswAccount;
        @BindView(R.id.psw_psw)
        TextView pswPsw;

        public PswViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void onLongItemClick(View view, int position) {

        }

        @Override
        public void onBindViewHolder(int position) {
            PswDaoModel userDaoModel = mDataList.get(position);
            pswTitle.setText(userDaoModel.getTitle());
            pswWebsit.setText(userDaoModel.getWebsite());
            pswAccount.setText(userDaoModel.getAccount());
            pswPsw.setText(userDaoModel.getPassword());
        }

        @Override
        public void onItemClick(View view, int position) {

        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(String event) {
        if("delAllPsw".equals(event)){
            mDataList.clear();
            adapter.notifyDataSetChanged();
        }
    }


    @Override
    public void onSuccess() {

    }

    @Override
    public void onFailure() {

    }
}
